package treeedit;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class CodeFiller extends JWindow {
	JList list = new JList();
	JEditorPane editor = new JEditorPane();
	String[] values;
	Vector<String> filtered = new Vector<String>();
	JScrollPane editorScroll = new JScrollPane(editor);
	Component that = this;

	public CodeFiller() {
		this.setLayout(new GridLayout(1,2));
		CodeDictionary dict = Lisper.getInstance().getDictionary();
		this.values = dict.getFunctionNames();
		setFocusableWindowState(false);
		setSize(500,300);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(new JScrollPane(list));
		editor.setContentType("text/html");
		editor.setEditable(false);
		editorScroll.setEnabled(false);
		add(editorScroll);
	}
	
	public void filter(String text) {
		Object selected = null;
		if(filtered.contains(list.getSelectedValue())) {
			selected = list.getSelectedValue();
		}
		filtered.clear();
		for(String word : Lisper.getInstance().getDictionary().getFunctionNames()) 
			if(word.startsWith(text))
				filtered.add(word);
		if(filtered.size()==0)
			setVisible(false);
		list.setListData(filtered);
		if(selected!=null)
			list.setSelectedValue(selected,true);
		updateEditor();
	}
	public String finish() {
		Object selected = "";
		if(filtered.contains(list.getSelectedValue())) {
			selected = list.getSelectedValue();
		}
		this.setVisible(false);
		return selected.toString();
	}
	
	public void attachTo(Component c)
	{
		Point sourceLocation = c.getLocationOnScreen();
		int sourceHeight = c.getHeight();
		setLocation(new Point(sourceLocation.x,sourceLocation.y+sourceHeight));
		Component root = SwingUtilities.getRoot(c);
//		System.out.println("root: " + root);
		WindowAdapter wa = new WindowAdapter() {
			public void windowStateChanged(WindowEvent evt) {
				that.setVisible(false);
				System.out.println(editorScroll.getViewport().getViewPosition());
			}
		};
		ComponentAdapter ca = new ComponentAdapter() {
			public void componentHidden(ComponentEvent e) {
				that.setVisible(false);
			}
			public void componentMoved(ComponentEvent e) {
				that.setVisible(false);
			}
			public void componentResized(ComponentEvent e) {
				that.setVisible(false);
			} 
			public void componentShown(ComponentEvent e){
				that.setVisible(false);
			}
		};
		((Frame)root).addWindowStateListener(wa);
		((Frame)root).addWindowFocusListener(wa);
		((Frame)root).addWindowListener(wa);
		root.addComponentListener(ca);

	}
	
	public void selectDown() {
		if(isVisible())
			if(filtered.contains(list.getSelectedValue()))
				list.setSelectedValue(filtered.get((list.getSelectedIndex()+1)%filtered.size()), true);
			else
				list.setSelectedIndex(0);
		updateEditor();
	}
	public void selectUp() {
		if(isVisible())
			if(filtered.contains(list.getSelectedValue()))
				list.setSelectedValue(filtered.get((list.getSelectedIndex()-1+filtered.size())%filtered.size()), true);
			else
				list.setSelectedIndex(filtered.size());
		updateEditor();
	}
	private void updateEditor() {
		String sel = ( (list.getSelectedValue()==null) ? "" : (String)list.getSelectedValue());
		editor.setText("<html>" + Lisper.getInstance().getDescription(sel) + "</html>");
		editor.scrollToReference("<html>");
		

		//editorScroll.getViewport().setViewPosition(new Point(0,0));
		//System.out.println(editorScroll.getViewport().getViewPosition());

		//editorScroll.getVerticalScrollBar().setValue(0);
		//editorScroll.getHorizontalScrollBar().setValue(0);

		//editorScroll.scrollRectToVisible(new Rectangle(0,0,5,5));
		//editor.scrollRectToVisible(new Rectangle(0,0,5,5));

		//editorScroll.repaint();
	}
}

